<?php

use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Media;
use Fisharebest\Webtrees\Note;
use Fisharebest\Webtrees\Repository;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Submitter;
use Fisharebest\Webtrees\View;
use Illuminate\Support\Collection;

/**
 * @var int                  $limit_high
 * @var int                  $limit_low
 * @var array<array<mixed>>  $order
 * @var Collection<stdClass> $rows
 * @var bool                 $show_date
 * @var bool                 $show_user
 */
?>

<div class="table-responsive">
    <table
        class="table table-sm wt-table-changes datatables d-none"
        <?= view('lists/datatables-attributes') ?>
        data-paging="<?= e(json_encode($rows->count() >= e($limit_high))) ?>"
        data-filter="false"
        data-info="false"
        data-length-change="false"
        data-order="<?= e(json_encode($order)) ?>"
        data-page-length="<?= e($limit_low) ?>"
    >
        <thead>
            <tr>
                <th class="wt-side-block-optional">
                <span class="sr-only">
                    <?= I18N::translate('Type') ?>
                </span>
                </th>
                <th>
                    <?= I18N::translate('Record') ?>
                </th>
                <th class="<?= $show_date ? '' : 'd-none' ?>">
                    <?= I18N::translate('Last change') ?>
                </th>
                <th class="<?= $show_user ? '' : 'd-none' ?>">
                    <?= I18N::translate('Editor') ?>
                </th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $row) : ?>
                <tr>
                    <td data-sort="<?= $row->record->tag() ?>" class="text-centre wt-side-block-optional">
                        <?php if ($row->record->tag() === Individual::RECORD_TYPE) : ?>
                            <?= view('icons/individual') ?>
                            <span class="sr-only"><?= I18N::translate('Individual') ?></span>
                        <?php elseif ($row->record->tag() === Family::RECORD_TYPE) : ?>
                            <?= view('icons/family') ?>
                            <span class="sr-only"><?= I18N::translate('Family') ?></span>
                        <?php elseif ($row->record->tag() === Media::RECORD_TYPE) : ?>
                            <?= view('icons/media') ?>
                            <span class="sr-only"><?= I18N::translate('Media') ?></span>
                        <?php elseif ($row->record->tag() === Note::RECORD_TYPE) : ?>
                            <?= view('icons/note') ?>
                            <span class="sr-only"><?= I18N::translate('Note') ?></span>
                        <?php elseif ($row->record->tag() === Source::RECORD_TYPE) : ?>
                            <?= view('icons/source') ?>
                            <span class="sr-only"><?= I18N::translate('Source') ?></span>
                        <?php elseif ($row->record->tag() === Submitter::RECORD_TYPE) : ?>
                            <?= view('icons/submitter') ?>
                            <span class="sr-only"><?= I18N::translate('Submitter') ?></span>
                        <?php elseif ($row->record->tag() === Repository::RECORD_TYPE) : ?>
                            <?= view('icons/repository') ?>
                            <span class="sr-only"><?= I18N::translate('Repository') ?></span>
                        <?php endif ?>
                    </td>

                    <td data-sort="<?= e($row->record->sortName()) ?>">
                        <a href="<?= e($row->record->url()) ?>"><?= $row->record->fullName() ?></a>
                    </td>

                    <td data-sort="<?= $row->time->timestamp ?>" class="<?= $show_date ? '' : 'd-none' ?>">
                        <?= view('components/datetime', ['timestamp' => $row->time]) ?>
                    </td>

                    <td class="<?= $show_user ? '' : 'd-none' ?>">
                        <?= e($row->user->userName()) ?>
                    </td>
                </tr>
            <?php endforeach ?>
        </tbody>
    </table>
</div>

<?php View::push('javascript') ?>
<script>
  $(".wt-table-changes").dataTable().removeClass("d-none");
</script>
<?php View::endpush() ?>
